import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:largefriends/entity/entities/lf_bool_entity.dart';

import '../../../config/lf_route.dart';
import '../../../net/lf_api_url.dart';
import '../../../net/lf_dio_util.dart';
import '../../../service/lf_auth_service.dart';
import '../../../utils/lf_dialog_utils.dart';
import '../../../utils/lf_keyboard.dart';
import '../../lf_payment/lf_payment_logic.dart';

toChangeUsername() {
  LfAuthService authService = Get.find();
  if (authService.currentAccount!.isGold()) {
    Get.toNamed(LFRoute.pathEditUsername);
  } else {
    toPayment();
  }
}

class LfChangeUsernameLogic extends GetxController {
  LfAuthService authService = Get.find();
  TextEditingController textEditingController = TextEditingController();
  FocusNode focusNode = FocusNode();
  RxString newName = "".obs;

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    closeSoftKeyboard();
    textEditingController.dispose();
    focusNode.dispose();
    super.onClose();
  }

  void changeUsername() {
    closeSoftKeyboard();
    showLoading();
    LfDio().post<LfBoolEntity>(
        url: apiChangeUserName,
        params: {'username': newName.value.trim()}).then((value) {
      hideLoading();
      authService.currentAccount!.username = newName.value.trim();
      authService.setUserEntity(authService.currentAccount!);
      Get.back();
    });
  }
}
